package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.ComplimentaryBooking;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.bo.Remark;
import org.hgvc.webapp.ets.bo.VoucherEntryDetail;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.BookingConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.BookingService;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.hgvc.webapp.ets.spring.service.VoucherService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class AdminComplimentaryBookingBean extends AbstractBean{
	public static final String BEAN_NAME="adminComplimentaryBookingBean";
	
	private BookingService bookingService;
	private VoucherService voucherService;
	private MasterSetupService masterSetupService;
	
	private List<VoucherEntryDetail> voucherEntryDetailList;
	private List<ComplimentaryBooking> complimentaryBookingList;
	private List<Remark> remarkList;
	private ComplimentaryBooking complimentaryBooking;
	private VoucherEntryDetail voucherEntryDetail;
	private PartnerProduct partnerProduct;
	private Remark remark;
	private List<SelectItem> selectConfirmProduct;
	private List<SelectItem> selectRemark;
	
	private boolean confirmBooking;
	private boolean cancelBooking;
	private boolean memberFlag;
	private boolean guestFlag;
	private boolean prospectFlag;
	private boolean catchError;
	private boolean lookupForm;
	private List<SelectItem> selectSearchList;
	private String searchField;
	private String searchKeyword;
	
	public String getSearchField() {
		return searchField;
	}
	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}
	public String getSearchKeyword() {
		return searchKeyword;
	}
	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}
	public List<SelectItem> getSelectSearchList() {
		return selectSearchList;
	}
	public boolean isLookupForm() {
		return lookupForm;
	}
	public void setLookupForm(boolean lookupForm) {
		this.lookupForm = lookupForm;
	}
	public void setVoucherService(VoucherService voucherService) {
		this.voucherService = voucherService;
	}
	public void setBookingService(BookingService bookingService) {
		this.bookingService = bookingService;
	}
	public void setMasterSetupService(MasterSetupService masterSetupService) {
		this.masterSetupService = masterSetupService;
	}
	public ComplimentaryBooking getComplimentaryBooking() {
		return complimentaryBooking;
	}
	public void setComplimentaryBooking(ComplimentaryBooking complimentaryBooking) {
		this.complimentaryBooking = complimentaryBooking;
	}
	public VoucherEntryDetail getVoucherEntryDetail() {
		return voucherEntryDetail;
	}
	public void setVoucherEntryDetail(VoucherEntryDetail voucherEntryDetail) {
		this.voucherEntryDetail = voucherEntryDetail;
	}
	public PartnerProduct getPartnerProduct() {
		return partnerProduct;
	}
	public void setPartnerProduct(PartnerProduct partnerProduct) {
		this.partnerProduct = partnerProduct;
	}
	public List<VoucherEntryDetail> getVoucherEntryDetailList() {
		return voucherEntryDetailList;
	}
	public List<ComplimentaryBooking> getComplimentaryBookingList() {
		return complimentaryBookingList;
	}
	public List<SelectItem> getSelectConfirmProduct() {
		return selectConfirmProduct;
	}
	public List<SelectItem> getSelectRemark() {
		return selectRemark;
	}
	public Remark getRemark() {
		return remark;
	}
	public void setRemark(Remark remark) {
		this.remark = remark;
	}
	public List<Remark> getRemarkList() {
		return remarkList;
	}
	
	public String initComplimentaryBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		
		try{
			this.memberFlag=false;
			this.confirmBooking=false;
			this.cancelBooking=false;
			this.complimentaryBooking=new ComplimentaryBooking();
			this.partnerProduct=new PartnerProduct();
			this.voucherEntryDetail=new VoucherEntryDetail();
			this.remark=new Remark();
			this.selectConfirmProduct=new ArrayList<SelectItem>();
			this.selectRemark=new ArrayList<SelectItem>();
			this.complimentaryBookingList=this.bookingService.getComplimentaryBookingList();
			
			this.remarkList=this.masterSetupService.getRemarkList();
			for(Remark remark : this.remarkList){
				SelectItem selectItem=new SelectItem(remark.getRemarkId(),remark.getCode()+" - "+remark.getDesc());
				this.selectRemark.add(selectItem);
			}
			this.searchField=BookingConstant.SEARCH_BOOKING_NO;
			this.searchKeyword=null;
			this.selectSearchList=new ArrayList<SelectItem>();
			SelectItem bnSelectItem=new SelectItem(BookingConstant.SEARCH_BOOKING_NO,BookingConstant.SEARCH_BOOKING_NO);
			this.selectSearchList.add(bnSelectItem);
			SelectItem cnSelectItem=new SelectItem(BookingConstant.SEARCH_CONFIRMATION_NO,BookingConstant.SEARCH_CONFIRMATION_NO);
			this.selectSearchList.add(cnSelectItem);
		}catch(BaseException be){
			initErrorBean(be);
		}
		return outcome;
	}
	
	public String navAddComplimentaryBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navAddComplimentaryBookingActionEvent(ActionEvent event){
		this.catchError=false;
		this.complimentaryBooking=new ComplimentaryBooking();
		this.partnerProduct=new PartnerProduct();
		this.voucherEntryDetail=new VoucherEntryDetail();
		this.remark=new Remark();
	}
	public String addComplimentaryBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void addComplimentaryBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.complimentaryBooking.setVoucherEntryDetail(this.voucherEntryDetail);
			this.complimentaryBooking.setVoucher(this.voucherEntryDetail.getVoucher());
			this.complimentaryBooking.setRemark(this.remark.getRemarkId().length()<=0?null:this.remark);
			this.complimentaryBooking.setConfirmProduct(this.partnerProduct);
			this.complimentaryBooking.setCreatedBy(getThisUser());
			this.complimentaryBooking.setCreatedDate(new Date());
			this.complimentaryBooking.setIsDeleted(CommonConstant.NOT_DELETED);
			if (this.complimentaryBooking.getConfirmationNo().length()>0 && this.complimentaryBooking.getConfirmationDate()!=null)
				this.complimentaryBooking.setStatus(BookingConstant.STATUS_CONFIRM);
			else
				this.complimentaryBooking.setStatus(BookingConstant.STATUS_PENDING);
			this.bookingService.saveComplimentaryBooking(this.complimentaryBooking);
			this.complimentaryBookingList=this.bookingService.getComplimentaryBookingList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String navEditComplimentaryBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void navEditComplimentaryBookingActionEvent(ActionEvent event){
		this.catchError=false;
		this.confirmBooking=false;
		this.cancelBooking=false;
		UIParameter param=(UIParameter)event.getComponent().findComponent("complimentaryBookingId");
		String complimentaryBookingId = param.getValue().toString();
		for (ComplimentaryBooking complimentaryBookingP : this.complimentaryBookingList){
			if (complimentaryBookingP.getComplimentaryBookingId().equalsIgnoreCase(complimentaryBookingId)){
				this.complimentaryBooking=complimentaryBookingP;
				this.partnerProduct=this.complimentaryBooking.getConfirmProduct();
				this.remark=this.complimentaryBooking.getRemark();
				if (this.remark==null)
					this.remark=new Remark();
				initSelectProductList(complimentaryBookingP.getVoucherEntryDetail());
				
				if (this.complimentaryBooking.getStatus().equalsIgnoreCase(BookingConstant.STATUS_CONFIRM))
					this.confirmBooking=true;
				if (this.complimentaryBooking.getStatus().equalsIgnoreCase(BookingConstant.STATUS_CANCEL))
					this.cancelBooking=true;
				break;
			}
		}
	}
	public String updateComplimentaryBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void updateComplimentaryBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			if (this.complimentaryBooking.getConfirmationNo().length()>0 && this.complimentaryBooking.getConfirmationDate()!=null)
				this.complimentaryBooking.setStatus(BookingConstant.STATUS_CONFIRM);
			this.complimentaryBooking.setModifiedBy(getThisUser());
			this.complimentaryBooking.setModifiedDate(new Date());
			this.complimentaryBooking.setConfirmProduct(this.partnerProduct);
			this.complimentaryBooking.setRemark(this.remark.getRemarkId().length()<=0?null:this.remark);
			this.bookingService.updateComplimentaryBooking(this.complimentaryBooking);
			this.complimentaryBookingList=this.bookingService.getComplimentaryBookingList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public void cancelComplimentaryBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.complimentaryBooking.setStatus(BookingConstant.STATUS_CANCEL);
			this.complimentaryBooking.setModifiedBy(getThisUser());
			this.complimentaryBooking.setModifiedDate(new Date());
			this.complimentaryBooking.setRemark(this.remark.getRemarkId().length()<=0?null:this.remark);
			this.bookingService.updateComplimentaryBooking(this.complimentaryBooking);
			this.complimentaryBookingList=this.bookingService.getComplimentaryBookingList();
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public String searchComplimentaryBookingAction(){
		String outcome=AdminConstant.NAV_STATUS_SUCCESS.concat("_"+getRoleName());
		if (this.catchError)
			outcome=null;
		return outcome;
	}
	public void searchComplimentaryBookingActionEvent(ActionEvent event){
		this.catchError=false;
		try{
			this.complimentaryBookingList=this.bookingService.getComplimentaryBookingList(this.searchField, this.searchKeyword);
		}catch(BaseException be){
			this.catchError=true;
			initErrorBean(be);
		}
	}
	
	public void lookupVoucherFormActionEvent(ActionEvent event){
		this.lookupForm=true;
		try{
			this.voucherEntryDetailList=this.voucherService.getVoucherEntryDetailAvailableList();
		}catch(BaseException be){
			initErrorBean(be);
		}
	}
	
	private void initSelectProductList(VoucherEntryDetail voucherEntryDetailParam){
		this.selectConfirmProduct=new ArrayList<SelectItem>();
		
		if (voucherEntryDetailParam.getVoucher().getProduct1()!=null){
			SelectItem selectItem=new SelectItem(voucherEntryDetailParam.getVoucher().getProduct1().getPartnerProductId(),
					voucherEntryDetailParam.getVoucher().getProduct1().getCode()+" - " + 
					voucherEntryDetailParam.getVoucher().getProduct1().getDesc());
			this.selectConfirmProduct.add(selectItem);
		}
		if (voucherEntryDetailParam.getVoucher().getProduct2()!=null){
			SelectItem selectItem=new SelectItem(voucherEntryDetailParam.getVoucher().getProduct2().getPartnerProductId(),
					voucherEntryDetailParam.getVoucher().getProduct2().getCode()+" - " + 
					voucherEntryDetailParam.getVoucher().getProduct2().getDesc());
			this.selectConfirmProduct.add(selectItem);
		}
		if (voucherEntryDetailParam.getVoucher().getProduct3()!=null){
			SelectItem selectItem=new SelectItem(voucherEntryDetailParam.getVoucher().getProduct3().getPartnerProductId(),
					voucherEntryDetailParam.getVoucher().getProduct3().getCode()+" - " + 
					voucherEntryDetailParam.getVoucher().getProduct3().getDesc());
			this.selectConfirmProduct.add(selectItem);
		}
		if (voucherEntryDetailParam.getVoucher().getProduct4()!=null){
			SelectItem selectItem=new SelectItem(voucherEntryDetailParam.getVoucher().getProduct4().getPartnerProductId(),
					voucherEntryDetailParam.getVoucher().getProduct4().getCode()+" - " + 
					voucherEntryDetailParam.getVoucher().getProduct4().getDesc());
			this.selectConfirmProduct.add(selectItem);
		}
		if (voucherEntryDetailParam.getVoucher().getProduct5()!=null){
			SelectItem selectItem=new SelectItem(voucherEntryDetailParam.getVoucher().getProduct5().getPartnerProductId(),
					voucherEntryDetailParam.getVoucher().getProduct5().getCode()+" - " + 
					voucherEntryDetailParam.getVoucher().getProduct5().getDesc());
			this.selectConfirmProduct.add(selectItem);
		}
	}
	
	public void lookupVoucherFormSelectedActionEvent(ActionEvent event){
		UIParameter param = (UIParameter)event.getComponent().findComponent("voucherEntryDetailId");
		String voucherEntryDetailId=param.getValue().toString();
		
		
		this.lookupForm=false;
		this.memberFlag=false;
		this.guestFlag=false;
		this.prospectFlag=false;
		for(VoucherEntryDetail voucherEntryDetailP : this.voucherEntryDetailList){
			if (voucherEntryDetailP.getVoucherEntryDetailId().equalsIgnoreCase(voucherEntryDetailId)){
				this.voucherEntryDetail=voucherEntryDetailP;
				initSelectProductList(voucherEntryDetailP);
				if (this.voucherEntryDetail.getVoucherEntry().getAgreement()!=null)
					this.memberFlag=true;
				if (this.voucherEntryDetail.getVoucherEntry().getGuest()!=null)
					this.guestFlag=true;
				if (this.voucherEntryDetail.getVoucherEntry().getProspect()!=null)
					this.prospectFlag=true;
				break;	
			}
		}
	}
	public void lookupVoucherFormCloseActionEvent(ActionEvent event){
		this.lookupForm=false;
	}
	public boolean isMemberFlag() {
		return memberFlag;
	}
	public void setMemberFlag(boolean memberFlag) {
		this.memberFlag = memberFlag;
	}
	public boolean isGuestFlag() {
		return guestFlag;
	}
	public void setGuestFlag(boolean guestFlag) {
		this.guestFlag = guestFlag;
	}
	public boolean isProspectFlag() {
		return prospectFlag;
	}
	public void setProspectFlag(boolean prospectFlag) {
		this.prospectFlag = prospectFlag;
	}
	public boolean isConfirmBooking() {
		return confirmBooking;
	}
	public void setConfirmBooking(boolean confirmBooking) {
		this.confirmBooking = confirmBooking;
	}
	public boolean isCancelBooking() {
		return cancelBooking;
	}
	public void setCancelBooking(boolean cancelBooking) {
		this.cancelBooking = cancelBooking;
	}
	
}
